<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-test.js"></script>
<script src="/bluetooth/resources/bluetooth-fake-devices.js"></script>
<script>
'use strict';
const test_desc = 'A regular write request to a writable descriptor ' +
    'should succeed.';
let typed_array = Uint8Array.of(1, 2);
let array_buffer = Uint8Array.of(3, 4).buffer;
let data_view = new DataView(new ArrayBuffer(2));
let descriptor, fake_descriptor;

bluetooth_test(() => getUserDescriptionDescriptor()
    .then(_ => ({descriptor, fake_descriptor} = _))
    .then(() => new Promise(resolve => {
        data_view.setUint8(0, 5);
        data_view.setUint8(1, 6);
        resolve();
    }))
    .then(() => fake_descriptor.getLastWrittenValue())
    .then(last_value => assert_true(last_value === null))

    .then(() => fake_descriptor.setNextWriteResponse(GATT_SUCCESS))
    .then(() => descriptor.writeValue(typed_array))
    .then(() => fake_descriptor.getLastWrittenValue())
    .then(last_value => assert_array_equals(last_value, [1, 2]))

    .then(() => fake_descriptor.setNextWriteResponse(GATT_SUCCESS))
    .then(() => descriptor.writeValue(array_buffer))
    .then(() => fake_descriptor.getLastWrittenValue())
    .then(last_value => assert_array_equals(last_value, [3, 4]))

    .then(() => fake_descriptor.setNextWriteResponse(GATT_SUCCESS))
    .then(() => descriptor.writeValue(data_view))
    .then(() => fake_descriptor.getLastWrittenValue())
    .then(last_value => assert_array_equals(last_value, [5, 6])),
    test_desc);
</script>
